package com.pachong.android.baseuicomponent.view;


import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pachong.android.baseuicomponent.R;

/**
 * 分为左/中/右 三个部分
 *
 * @author Veivei Peng
 */
public class CenterToolbar extends LinearLayout {
    private static final String TAG = CenterToolbar.class.getSimpleName();

    private Drawable mBackDrawable;

    public CenterToolbar(Context context) {
        this(context, null);
    }

    public CenterToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.toolbar, this);

        int[] attrsArray = {R.attr.toolbarBackDrawable};
        TypedArray typedArray = context.obtainStyledAttributes(attrsArray);
        mBackDrawable = typedArray.getDrawable(0);
        typedArray.recycle();

        if (mBackDrawable == null) {
            Log.w(TAG, "Toolbar warning：没有配置返回按钮图标");
        }
    }


    //region  Set title
    public void setTitle(@StringRes int strId) {
        String text = getContext().getString(strId);
        setTitle(text);
    }

    public void setTitle(CharSequence str) {
        setTitle(str, -1, null);
    }

    public void setTitle(CharSequence str, int color) {
        setTitle(str, color, null);
    }

    public TextView setTitle(CharSequence str, OnClickListener listener) {
        return setTitle(str, -1, listener);
    }


    public TextView setTitle(CharSequence str, int color, OnClickListener listener) {
        TextView tvTitle = (TextView) findViewById(android.R.id.title);
        if (tvTitle == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.toolbar_center_text, null);
            setCenterView(view);
            tvTitle = (TextView) findViewById(android.R.id.title);
        }

        if (tvTitle != null) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(str);
            tvTitle.setClickable(true);
            tvTitle.setOnClickListener(listener);
            tvTitle.setCompoundDrawables(null, null, null, null);
            if (color > 0) {
                tvTitle.setTextColor(color);
            }
        }
        return tvTitle;
    }

    // endregion Set title

    public void setCenterView(View view) {
        LinearLayout llyCenter = (LinearLayout) this.findViewById(R.id.lytCenter);
        llyCenter.removeAllViews();
        llyCenter.addView(view);
    }


    //region 添加ImageButton
    private ImageButton addImageButton(int side, Drawable drawable, OnClickListener listener) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ImageButton ibtn = (ImageButton) inflater.inflate(R.layout.toolbar_image_button, getLeftButtonContainer(), false);

        ibtn.setImageDrawable(drawable);
        if (listener != null) {
            ibtn.setOnClickListener(listener);
        }
        return (ImageButton) addView(side, ibtn);
    }


    public ImageButton addRightImageButton(Drawable drawable, OnClickListener listener) {
        return addImageButton(SIDE_RIGHT, drawable, listener);
    }

    public ImageButton addLeftImageButton(Drawable drawable, OnClickListener listener) {
        return addImageButton(SIDE_LEFT, drawable, listener);
    }

    public ImageButton addRightImageButton(@DrawableRes int drawableId, OnClickListener listener) {
        return addImageButton(SIDE_RIGHT, getContext().getResources().getDrawable(drawableId), listener);
    }

    public ImageButton addLeftImageButton(@DrawableRes int drawableId, OnClickListener listener) {
        return addImageButton(SIDE_LEFT, getContext().getResources().getDrawable(drawableId), listener);
    }
    // endregion

    // region 添加Button
    private Button addButton(int side, CharSequence chars, OnClickListener listener) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Button btn = (Button) inflater.inflate(R.layout.toolbar_button, getLeftButtonContainer(), false);

        btn.setText(chars);
        if(chars.equals("注册")){
            btn.setTextSize(14);
        }
        if (listener != null) {
            btn.setOnClickListener(listener);
        }
        return (Button) addView(side, btn);
    }


    public Button addRightButton(CharSequence chars, OnClickListener listener) {
        return addButton(SIDE_RIGHT, chars, listener);
    }

    public Button addLeftButton(CharSequence chars, OnClickListener listener) {
        return addButton(SIDE_LEFT, chars, listener);
    }

    public Button addRightButton(@StringRes int strId, OnClickListener listener) {
        return addButton(SIDE_RIGHT, getContext().getString(strId), listener);
    }

    public Button addLeftButton(@StringRes int strId, OnClickListener listener) {
        return addButton(SIDE_LEFT, getContext().getString(strId), listener);
    }
    // endregion

    //region 添加自定义view

    public View addRightView(View view) {
        return addView(SIDE_RIGHT, view);
    }

    public View addLeftView(View view) {
        return addView(SIDE_LEFT, view);
    }

    private View addView(int side, View customView) {
        if (side == SIDE_LEFT) {
            getLeftButtonContainer().addView(customView);
        } else {
            getRightButtonContainer().addView(customView, 0);
        }

        return customView;
    }

    // endregion

    public ImageButton enableBackButton() {
        if (mBackDrawable == null) {
            throw new RuntimeException("没有设置返回键的图标");
        }
        ImageButton ibtn = addLeftImageButton(mBackDrawable, new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getContext() instanceof Activity) {
                    Activity actContext = (Activity) getContext();
                    actContext.finish();
                }
            }
        });
        return ibtn;
    }


    public void clearRight() {
        getRightButtonContainer().removeAllViews();

    }

    public void clearLeft() {
        getLeftButtonContainer().removeAllViews();

    }


    // ===============>  private function  <====================

    private final static int SIDE_LEFT = 0X1;
    private final static int SIDE_RIGHT = 0X2;

    private final static int TYPE_TEXT = 0X1;
    private final static int TYPE_IMAGE = 0X2;


    /**
     * 右边按钮的容器
     */
    public LinearLayout getRightButtonContainer() {
        return (LinearLayout) findViewById(R.id.lytRightBtn);
    }

    /**
     * 左边按钮的容器
     */
    public LinearLayout getLeftButtonContainer() {
        return (LinearLayout) findViewById(R.id.lytLeftBtn);
    }

    @Override
    public void setBackground(Drawable background) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            findViewById(R.id.rlytContent).setBackground(background);
        } else {
            Log.w(TAG, "setBackground: API 版本必须 大于 16");
        }
    }

    @Override
    public void setBackgroundColor(int color) {
        findViewById(R.id.rlytContent).setBackgroundColor(color);
    }


    @Override
    public void setBackgroundResource(int resid) {
        findViewById(R.id.rlytContent).setBackgroundResource(resid);
    }

    @Override
    public void setBackgroundDrawable(Drawable background) {
        findViewById(R.id.rlytContent).setBackgroundDrawable(background);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


}
